

#ifndef __BASEITEMMODEL_H__
#define __BASEITEMMODEL_H__

#include <QAbstractItemModel>
#include <QList>
#include <QMenu>

class BaseItem;
class BaseItemModel;
class BaseItem : public QObject
{
  Q_OBJECT
private:
  QList<BaseItem*>  _children;
  BaseItem*         _parent;

  BaseItemModel*    _model;

  void*             _data;
protected:
  void SetData (void* data);

public:
  BaseItem (BaseItem* parent = 0);

  BaseItem* Find (void* data);

  /** Adds a new item and informs the model */
  virtual void Add (BaseItem* item);
  /** Removes the given item and informs the model */
  virtual void Remove (BaseItem* item);

  void SetModel (BaseItemModel* model);
  BaseItemModel* GetModel ();

  int GetNumberOfItems () const;
  int GetIndexOf (BaseItem* item);
  int GetIndex ();

  BaseItem* GetItem (int idx);
  BaseItem* GetParent ();

  /** Return the visible information */
  virtual QVariant GetData (int column, int role = Qt::DisplayRole);


  /** Activates all Actions that are accessible from this item */
  virtual void EnableActions ();

  /** Add the menu items to the given menu */
  virtual bool BuildMenu (QMenu* menu);
  /** Call the default action */
  virtual void Trigger ();

  virtual QString GetTypeName () const { return "BaseItem"; }

public slots:
  /** Informs the model, that the data has changed */
  void TriggerDataChanged ();

  /** Removes this item from the own paren */
  void RemoveSelf ();

};



class BaseItemModel : public QAbstractItemModel
{
private:
  BaseItem*  _root;

  int         _columns;

public:
  BaseItemModel();

  BaseItem* GetRoot ();
  BaseItem* GetItem (const QModelIndex& index);

  void SetNumberOfColumns (int numberOfColumns);
  int GetNumberOfColumns () const;

  void DataChanged (BaseItem* item);
  void AddItem (BaseItem* item);
  void RemoveItem (BaseItem* item);

  QModelIndex CreateModelIndex (BaseItem* item, int column = 0) const;

  virtual int columnCount (const QModelIndex& parent = QModelIndex()) const;
  virtual QVariant data (const QModelIndex& index, int role = Qt::DisplayRole) const;
  virtual QModelIndex index (int row, int column, const QModelIndex& parent = QModelIndex()) const;
  virtual QModelIndex parent (const QModelIndex& index) const;
  virtual int rowCount (const QModelIndex& parent = QModelIndex()) const;

};

#endif /* ! __BASEITEMMODEL_H__ */
